#include "radioplaylistmodel.h"
#include "radiomodel.h"
#include "songmodel.h"
#include "BLL/radioBLl/radiobll.h"

RadioPlayListModel::RadioPlayListModel()
{
    _bll = 0;
}
RadioPlayListModel::~RadioPlayListModel()
{

    if(_bll){
       delete _bll;
    }
}

void RadioPlayListModel::setPlayQueueType(const QUEUE_TYPE &/*type*/)
{
//    PlayListBaseModel::setPlayQueueType(type);
    return;
}
RadioModel *RadioPlayListModel::radio() const
{
    return _radio;
}

void RadioPlayListModel::setRadio(RadioModel *radio)
{
    _radio = radio;
}
void RadioPlayListModel::setCurIndex(int value)
{
    PlayListBaseModel::setCurIndex(value);
    checkAndGetSongs(curIndex);
    qDebug()<< curIndex << value;
}

SongModel* RadioPlayListModel::nextSong(int *outIndex)
{
    SongModel *song = PlayListBaseModel::nextSong(outIndex);
    checkAndGetSongs(curIndex+1);
    return song;
}

SongModel* RadioPlayListModel::previoursSong(int *outIndex)
{
    SongModel *song = PlayListBaseModel::previoursSong(outIndex);
    checkAndGetSongs( (curIndex-1+count())%count() );
    return song;
}

SongModel* RadioPlayListModel::curSong()
{
    SongModel *song = PlayListBaseModel::curSong();
    checkAndGetSongs();
    return song;
}

void RadioPlayListModel::checkAndGetSongs(int  index)
{
    int checkIndex = index<0? getCurIndex():index;
    if(count()==0 || checkIndex>=count()-3){
        wantGetMoreSong();
    }
}


int RadioPlayListModel::wantGetMoreSong()
{
    if(_bll!=0){
        return 1;
    }
    _bll = new RadioBll();
    connect(_bll,SIGNAL(getSongsFinish_SIG(EZTally::EZTallyAutoPtr,int)),this,SLOT(onGetSongFinish_SLOT(EZTally::EZTallyAutoPtr,int)));
    _bll->setCurRadio(radio());
    int code = _bll->getSongs();

    if(code < 0){
        return code;
    }

    return 0;
}
void RadioPlayListModel::onGetSongFinish_SLOT(EZTally::EZTallyAutoPtr ptr,int error)
{
    RadioBll *sbll = (RadioBll*)sender();
    if(sbll!=_bll){
        return;
    }
    if(error!=0){
        return;
    }

    EZTally::EZTallyAutoPtr oldPtr;
    EZTally::EZTallyArray<SongModel*> *oldSongs = new EZTally::EZTallyArray<SongModel*>();
    oldPtr.Set(oldSongs);
    for(int i=0;i<this->songs->Count();i++){
        oldSongs->AddObject(this->songs->ObjectAtIndex(i));
    }

     EZTally::EZTallyArray<SongModel*> *newSongs = (EZTally::EZTallyArray<SongModel*>*)ptr.Get();
     for(int i=0;i<newSongs->Count();i++){
         SongModel *song = newSongs->ObjectAtIndex(i);
         this->songs->AddObject(song);
//         qDebug() << song->title();
     }

     for(int i=0;i<_listDelegate.count();i++){
         PlayListBaseModelDelegate *delegate = _listDelegate.at(i);
         delegate->onSongChange(this,oldSongs);
     }
     emit songsChange(oldPtr);

     oldSongs->Release();

     disconnect(_bll,0,0,0);
     delete _bll;
     _bll = 0;
}

void RadioPlayListModel::onGetSongsFinish(RadioBll*bll,EZTally::EZTallyArray<SongModel*> *songs,int error)
{
    if(bll!=_bll){
        return;
    }
    if(error!=0){
        return;
    }
    this->songs->Clear();
    EZTally::EZTallyArray<SongModel*> *newSongs = songs;
    for(int i=0;i<newSongs->Count();i++){
        this->songs->AddObject(newSongs->ObjectAtIndex(i));
    }
    delete _bll;
    _bll = 0;
}
